﻿using System;
using System.Web.UI.WebControls;
using Resources;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;
using Yglu.BusinessLayer.Enums;
using Yglu.BusinessLayer.Reports;

namespace Yglu.WebAdmin
{
	public partial class Stores_aspx : BasePage
	{
		#region Main methods

		protected void Page_Load(object sender, EventArgs e)
		{
			CheckPermissions(AdminLevel.Company);

			if (!IsPostBack)
			{
				PanEdit.Visible = false;

				SetDefaults();
			}
		}

		protected void Page_PreRender(Object sender, EventArgs e)
		{
			if (PanGrid.Visible)
			{
				LoadData();
			}

			PlaceReports.Visible = PanEdit.Visible && TxtStoreId.Text != "0";
		}

		private void SetDefaults()
		{
			SetDefaults(GridStores);
			SetDefaults(GridReports);

			TxtStoreId.Enabled = false;

			if (SessionManager.LoggedUser.AdminLevel < AdminLevel.All)
			{
				ButAddNew.Visible = false;
			}
		}

		private void LoadData()
		{
			GridStores.DataSource = new StoreCollection(DataAccess.Store_Get());
			GridStores.DataBind();

			LblGridCount.Text = GetLocalResourceObject("LblGridCount.Text").ToString().Replace("#", GridStores.Rows.Count.ToString());

			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion

		#region Add and edit

		protected void ButAddNew_Click(Object sender, EventArgs e)
		{
			TxtStoreId.Text = "0";
			TxtStoreTitle.Text = String.Empty;
			TxtStoreLocation.Text = String.Empty;
			ChkStoreActive.Checked = true;

			LitEditLegend.Text = GetLocalResourceObject("LitAddStore.Text").ToString();
			PanEdit.Visible = true;
			PanGrid.Visible = false;
		}

		protected void GridStores_RowCommand(Object sender, GridViewCommandEventArgs e)
		{
			EditStore(Convert.ToInt32(e.CommandArgument));
		}

		private void EditStore(Int32 row)
		{
			Int32 id = Convert.ToInt32(GridStores.Rows[row].Cells[0].Text);
			Store s = new Store(id);

			TxtStoreId.Text = s.Id.ToString();
			TxtStoreTitle.Text = s.Title;
			TxtStoreLocation.Text = s.Location;
			ChkStoreActive.Checked = s.Active;

            LitEditLegend.Text = GetLocalResourceObject("LitEditStore.Text").ToString();
			PanEdit.Visible = true;
			PanGrid.Visible = false;

			LoadReports(s);
		}

		private void LoadReports(Store s)
		{
			StoreOrdersCollection reports = ReportManager.GetStoreOrders(s.Id);

			GridReports.DataSource = reports;
			GridReports.DataBind();
		}

		#endregion

		#region Save to database

		protected void ButSave_Click(Object sender, EventArgs e)
		{
			Page.Validate();

			if (!Page.IsValid)
			{
				return;
			}

			Store s = new Store(Convert.ToInt32(TxtStoreId.Text));

			try
			{
				s.Title = TxtStoreTitle.Text.Trim();
				s.Location = TxtStoreLocation.Text.Trim();
				s.Active = ChkStoreActive.Checked;

				try
				{
					DataAccess.Store_Upsert(s);

					TxtStoreId.Text = s.Id.ToString();

					ClientScript.RegisterStartupScript(typeof(String), "StoreSaved", "YgluTopAlert('" + Messages.ConfirmationDataSaved + "', true);", true);

					PanGrid.Visible = true;
				}
				catch (Exception ex)
				{
					Master.SetError(ex, Messages.ErrorCouldNotSaveData);
				}
			}
			catch (Exception ex)
			{
				Master.SetError(ex, Messages.ErrorInvalidForm);
			}
		}

		protected void ButCancel_Click(Object sender, EventArgs e)
		{
			PanEdit.Visible = false;
			PanGrid.Visible = true;
		}

		#endregion
	}
}